# 准备工作
import requests
import json
import pandas as pd
key ="5bd1ca4e7bc885ff80882a4f8b45093b"
# 地理编码
# 地理编码函数
def geocode(key,address,city=None,batch=None,sig=None,output=None,callback=None):
    url="https://restapi.amap.com/v3/geocode/geo?parameters"   
    params ={
    "key":key,
    "address":address,
    "city":city,
    "batch":batch,
    "sig":sig,
    "output":output,
    "callback":callback
    r= requests.get(url=url,params=params)
    a_location=r.json()
    return a_location
# 关键字参数不可以放在位置参数前，位置参数一定要放在前面，关键字参数放在末尾！！！
    


# 逆地理编码函数
def regeo(key,location,poitype=None,radius=None,extensions=None,batch=None,roadlevel=None,sig=None,output=None,callback=None,homercorp=None):
    regeo_url="https://restapi.amap.com/v3/geocode/regeo?parameters"
    params={
    "key":key,
    "location":location,
    "poitype":poitype,
    "radius":radius,
    "extensions":extensions,
    "batch":batch,
    "roadlevel":roadlevel,
    "sig":sig,
    "output":output,
    "callback":callback,
    "homercorp":homercorp
}
    r=requests.get(regeo_url,params=params)
    results = r.json()
    return results



# 步行路径规划函数
def geocode(key,address,sig=None,output=None,callback=None)-> str:
    # 获取高德api的地理编码，注：key指高德API 的密钥，请自行注册
    # addition指结构化地址
    url="https://restapi.amap.com/v3/geocode/geo?parameters"   
    params ={
    "key":key,
    "address":address,
    "sig":sig,
    "callback":callback,
    "output":"json"
}
    r = requests.get(url=url,params=params)
    results=r.json()['geocodes'][0]['location']
    return results



# 公交路径规划函数
def transit(key,origin,destination,city,cityd=None,extensions=None,strategy=None,nightflag=None,date=None,time=None,sig=None,output=None,callback=None):
    route_url="https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params={
    "key":key,
    "origin":origin,
    "destination":destination,
    "city":city,
    "cityd":cityd,
    "extensions":extensions,
    "strategy":strategy,
    "nightflag":nightflag,
    "date":date,
    "time":time,
    "sig":sig,
    "callback":callback,
    "output":"json"
}
    r=requests.get(route_url,params=params)
    transit=r.json()[ 'route']['transits'][0][ 'segments'][0]['bus']['buslines']
    return transit



# 驾车路径规划函数
def dirving(key,origin,destination,extensions=None,nosteps=None,roadaggregation=None,ferry =None,cartype=None,number=None,province=None,avoidroad=None,avoidpolygons=None,waypoints=None,strategy=0,destinationtype=None,origintype=None,destinationid=None,originid=None,sig=None,output="JSON",callback=None):  
    route_url="https://restapi.amap.com/v3/direction/driving?parameters"
    params={
    "key":key,
    "origin":origin,
    "destination":destination,
    "extensions":extensions,
    "nosteps":nosteps,
    "roadaggregation":roadaggregation,
    "ferry":ferry,
    "cartype":cartype,
    "number":number,
    "province":province,
    "avoidroad":avoidroad,
    "avoidpolygons":avoidpolygons,
    "waypoints":waypoints,
    "strategy":strategy,
    "destinationtype":destinationtype,
    "originid":originid,
    "sig":sig,
    "callback":callback,
    "output":"json"     
}
    r=requests.get(route_url,params=params)
    dirving=r.json()[ 'route']['paths'][0][ 'steps']
    return dirving

# 货车路径规划函数
def truck(key,origin,destination,size=1,diu=None,strategy=1,waypoints=None,height=1.6,width=2.5,load=10,weight=0.9,axis=2,province=None,number=None,cartype=0,avoidpolygons=None,showpolyline=1,nosteps=0):
    truckURL="https://restapi.amap.com/v4/direction/truck?parameters"
    params={
    "key":key,
    "origin":origin,
    "destination":destination,
    "size":size,
    "diu":diu,
    "strategy":strategy,
    "waypoints":waypoints,
    "height":height,
    "width":width,
    "load":load,
    "weight":weight,
    "axis":axis,
    "province":province,
    "number" :number,
    "cartype":cartype,
    "avoidpolygons":avoidpolygons,
    "showpolyline":showpolyline,
    "nosteps":nosteps
}
    r=requests.get(truckURL,params=params)
    truck=r.json()
    return truck


# 距离测量函数
def distance(key,origins,destination,sig=None,output=None,callback=None,type=None):
    distance_url="https://restapi.amap.com/v3/distance?parameters"
    params={
        "key":key,
        "origins":origins,
        "destination":destination,
        "sig":sig,
        "type":type,
        "callback":callback,
        "output":output
}
    r=requests.get(distance_url,params=params)
    distance=r.json()[ 'results'][0]['distance']
    return distance

# 行政区域查询
def district(key,keywords=None,subdistrict=None,page=None,offset=None,extensions=None,filter=None,callback=None,output=None): 
    url="https://restapi.amap.com/v3/config/district?parameters"
    params={
    "key":key,
    "keywords":keywords,
    "output":"json",
    "subdistrict":subdistrict,
    "page":page,
    "offset":offset,
    "extensions":extensions,
    "filter":filter,
    "callback":callback
}
    r=requests.get(url,params=params)
    district=r.json()[ 'districts'][0][ 'districts']
    return district




# 搜索POI
def place(key,keywords=None,city=None,citylimit=None,children=None,offset=None,page=None,extensions=None,sig=None,output=None,callback=None):
    place_url="https://restapi.amap.com/v3/place/text?parameters"
    params={
     "key":key,
    "keywords":keywords,
    "city":city,
    "citylimit":citylimit,
    "children":children,
    "offset":offset,
    "page":page,
    "extensions":extensions,
    "sig":sig,
    "callback":callback,
    "output":"json"
}
    r=requests.get(place_url,params=params)
    place=r.json()
    return place




# 周边搜索函数
def around(key,location,keywords=None,types=None,city=None,offset=20,page=1,extensions="base",sig=None,output=None,callback=None):
    around_url="https://restapi.amap.com/v3/place/around?parameters"
    params={
        "key":key,
        "location":location,
        "keywords":keywords,
        "types":types,
        "city":city,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "callback":callback,
        "output":"json"
}
    r=requests.get(around_url,params=params)
    around=r.json()
    return around



# IP定位函数
def ip(key,ip,sig=None,output=None):
    url="https://restapi.amap.com/v3/ip?parameters"
    params={
    "key":key,
    "sig":sig,
    "output":output
}
    r=requests.get(url,params=params)
    ip=r.json()
    return ip




# 静态地图数据准备
from PIL import Image
from io import BytesIO
地图_location  = geocode(key,origin)
地图_location
import json

# 静态地图函数
def staticmap(key, location, zoom=15, size="mid", scale=1, markers=None, labels=None, paths=None, traffic=0, sig=None):
    staticmap_url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key": key,
        "location": 地图_location,
        "zoom": zoom,
        "size": size,
        "scale": scale,
        "markers": markers,
        "labels": labels,
        "paths": paths,
        "traffic": traffic,
        "sig": sig

    }
    r = requests.get(staticmap_url, params=params)
    result = r.content
    results = Image.open(BytesIO(r.content))
    return results



# 天气查询函数
def weather(key,city,extensions=None,output=None):
    weather_url="https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params={
        "key":key,
        "city":city,
        "extensions":extensions,
        "output":output
    }
    r=requests.get(weather_url,params=params)
    weather=r.json()['lives'][0]
    return pd.json_normalize(weather)



# 输入提示函数
def assistant(key,keywords,type=None,location=None,city=None,citylimit=None,datatype=None,sig=None,output=None,callback=None):
    assistant_url="https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params={
        "key":key,
        "keywords":keywords,
        "type":type,
        "location":location,
        "city":city,
        "citylimit":citylimit,
        "datatype":datatype,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r=requests.get(assistant_url,params=params)
    assistant=r.json()['tips']
    return assistant



